package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.example.pojo.entity.SysMenu;
import org.example.pojo.entity.SysRole;
import org.example.pojo.entity.SysRoleMenu;
import org.example.pojo.entity.SysUser;
import org.example.pojo.req.RoleMenuReq;
import org.example.pojo.vo.RoleVo;
import org.example.service.SysMenuService;
import org.example.service.SysRoleMenuService;
import org.example.service.SysRoleService;
import org.example.mapper.SysRoleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 妖妖零
 * @description 针对表【sys_role(角色信息表)】的数据库操作Service实现
 * @createDate 2024-05-06 20:44:43
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
        implements SysRoleService {

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Autowired
    private SysMenuService sysMenuService;


    /**
     * 获取角色列表数据
     *
     * @param page
     * @param size
     * @param roleName
     * @param roleKer
     * @param status
     * @param dateTime
     * @return
     */
    @Override
    public Page<RoleVo> pageList(Integer page, Integer size, String roleName, String roleKer, Integer status, String dateTime) {
        Page<SysRole> pages = new Page<>(page, size);
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper();
        if (StringUtils.isNoneBlank(roleName)) {
            wrapper.like(SysRole::getRoleName, roleName);
        }
        if (StringUtils.isNoneBlank(roleKer)) {
            wrapper.like(SysRole::getRoleKey, roleKer);
        }
        if (status != null) {
            wrapper.eq(SysRole::getStatus, status);
        }
        if (StringUtils.isNoneBlank(dateTime)) {
            wrapper.eq(SysRole::getCreateTime, dateTime);
        }
        Page<SysRole> sysUserPage = baseMapper.selectPage(pages, wrapper);

        //添加菜单信息
        List<RoleVo> roleVoList = new ArrayList<>();
        for (SysRole record : sysUserPage.getRecords()) {
            RoleVo roleVo = new RoleVo();
            BeanUtils.copyProperties(record, roleVo);
            LambdaQueryWrapper<SysRoleMenu> wrapperRoleMenu = new LambdaQueryWrapper<>();
            wrapperRoleMenu.eq(SysRoleMenu::getRoleId, record.getRoleId());
            List<SysRoleMenu> sysRoleMenus = sysRoleMenuService.list(wrapperRoleMenu);
            if (!CollectionUtils.isEmpty(sysRoleMenus)) {
                List<Long> menuIds = sysRoleMenus.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
                List<SysMenu> menus = sysMenuService.listByIds(menuIds);
                roleVo.setMenus(menus);
            }
            roleVoList.add(roleVo);
        }

        //封装新对象
        Page<RoleVo> roleVoPage = new Page<>();
        roleVoPage.setRecords(roleVoList);
        roleVoPage.setPages(sysUserPage.getPages());
        roleVoPage.setSize(sysUserPage.getSize());
        roleVoPage.setTotal(sysUserPage.getTotal());

        return roleVoPage;

    }

    /**
     * 修改角色菜单信息
     *
     * @param roleMenuReq
     */
    @Override
    public void editRoleMenu(RoleMenuReq roleMenuReq) {
        //编辑角色信息
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(roleMenuReq, sysRole);
        this.updateById(sysRole);
        //先删除相关的菜单角色关联信息
        LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleMenu::getRoleId, roleMenuReq.getRoleId());
        List<SysRoleMenu> roleMenus = sysRoleMenuService.list(wrapper);
        if (!CollectionUtils.isEmpty(roleMenus)) {
            sysRoleMenuService.remove(wrapper);
        }
        //保存新的关联关系
        for (String id : roleMenuReq.getMenuIds()) {
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setRoleId(roleMenuReq.getRoleId());
            roleMenu.setMenuId(Long.valueOf(id));
            sysRoleMenuService.save(roleMenu);
        }
    }

    /**
     * 删除角色
     *
     * @param roleId
     */
    @Override
    public void removeRole(String roleId) {
        SysRole role = this.getById(roleId);
        if (role != null) {
            this.removeById(roleId);
            //删除关系表信息数据
            LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysRoleMenu::getRoleId, roleId);
            List<SysRoleMenu> roleMenus = sysRoleMenuService.list(wrapper);
            if (!CollectionUtils.isEmpty(roleMenus)) {
                sysRoleMenuService.remove(wrapper);
            }
        }

    }

    /**
     * 获取角色集合
     *
     * @param roleIds
     * @return
     */
    @Override
    public List<String> getUSerRoleName(List<Long> roleIds) {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SysRole::getRoleId, roleIds);
        List<SysRole> roleList = this.list(wrapper);
        List<String> rolenKeyList = roleList.stream().map(SysRole::getRoleKey).collect(Collectors.toList());
        return rolenKeyList;
    }

    /**
     * 添加角色信息
     *
     * @param roleMenuReq
     */
    @Override
    public void saveRole(RoleMenuReq roleMenuReq) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(roleMenuReq, sysRole);
        sysRole.setRoleSort(1);
        this.save(sysRole);
        //保存角色菜单关系
        for (String menuId : roleMenuReq.getMenuIds()) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(sysRole.getRoleId());
            sysRoleMenu.setMenuId(Long.valueOf(menuId));
            sysRoleMenuService.save(sysRoleMenu);
        }
    }
}




